const db = wx.cloud.database()
let OPENID = '',
    id = '';

// pages/wordBook/wordBook.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        DataList: [],
        pageIndex: 1,
        pageSize: 20 // 一页显示条数
    },
    /**
     * 单词详情
     */
    transfer(e) {
        console.log('e', e)
        id = e.target.dataset.val;
        console.log('id：', id)

        wx.navigateTo({ // 暂定为跳转提示页面
            url: '/pages/dictionary/wordDetail/wordDetail?content=' + id,
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
        if (wx.getStorageSync('userInfo')) {
            OPENID = wx.getStorageSync('userInfo').openid
        } else {
            wx.showToast({
                title: '请登录',
                icon: "none"
            })
        }
        this.getList()
    },

    scrolltolower() { // 触底
        this.setData({
            pageIndex: this.data.pageIndex += 1
        }, () => {
            this.getList()
        })
    },

    async getList() {
        let {
            result
        } = await wx.cloud.callFunction({
            name: 'getDictWord',
            data: {
                type: 'starData',
                openid: OPENID
            }
        })
        if(result[0].starData.length == 0){
            wx.showToast({
              title: '还没有收藏单词哦',
              icon:'none'
            })
        }else{
            let starList = result[0].starData.reverse()
            this.setData({
                DataList: starList
            })
        } 
    },

    async moveStar(e) {
        let that = this
        let val = e.target.dataset.val;
        console.log('val', val)
        await wx.showModal({
            title: '确定删除这个单词吗',
            success(res) {
                if (res.confirm) {
                    wx.showLoading()
                    wx.cloud.callFunction({
                        name: 'removeDictWord',
                        data: {
                            type: 'rvmStar',
                            id: val
                        }
                    }).then(() =>{
                        wx.hideLoading()
                        that.getList()
                    })
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    }
})